package histogramaPalabras;

import java.awt.Color;
import java.awt.Dimension;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 *
 * @author Luis Felipe Gomez
 */
public class histogramaPalabras extends ApplicationFrame {

    public histogramaPalabras(String titulo, String palabra1, String palabra2, String palabra3, String palabra4, String palabra5,
            double valor1, double valor2, double valor3, double valor4, double valor5) {
        super(titulo);
        CategoryDataset dataset = crearDataset(palabra1, palabra2, palabra3, palabra4, palabra5, valor1, valor2, valor3, valor4, valor5);
        JFreeChart chart = crearChart(dataset);
        ChartPanel panel = new ChartPanel(chart, false);
        panel.setPreferredSize(new Dimension(700, 500));
        this.setContentPane(panel);

    }

    public static CategoryDataset crearDataset(String a, String b, String c, String d, String e, double f, double g, double h, double i, double j) {
        /*  String dato1= a  ; 
        String dato2 = b ; 
        String dato3= c ; 
        String dato4= d ; 
        String dato5= e ; */
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(f, a, "1");
        dataset.addValue(g, b, "2");
        dataset.addValue(h, c, "3");
        dataset.addValue(i, d, "4");
        dataset.addValue(j, e, "5");
        return dataset;

    }

    private static JFreeChart crearChart(CategoryDataset data) {
        
        JFreeChart chart = ChartFactory.createStackedBarChart("Histograma de palabras", "Palabra", "Porcentaje ", data, PlotOrientation.VERTICAL, true, true, true);
        chart.setBackgroundPaint(Color.ORANGE);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.BLUE);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.BLACK);
      final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));

       
        return chart;
    }

    public void ejecutarHistograma(histogramaPalabras palabraValores) {

        palabraValores.pack();
        RefineryUtilities.centerFrameOnScreen(palabraValores);
        palabraValores.setVisible(true);
    }
}
